<template>
  <div id="global-header">
    <a-row :wrap="false">
      <a-col flex="200px">
        <div class="title-bar">
          <img class="logo" src="../assets/logo.png" alt="logo" />
          <div class="title">用户中心</div>
        </div>
      </a-col>
      <a-col flex="auto">
        <a-menu v-model:selectedKeys="current" mode="horizontal" :items="items" @click="menuClick" />
      </a-col>
      <a-col flex="80px">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
            <a-dropdown :trigger="['click']">
              <div class="avatar-wrapper">
                <img :src="loginUserStore.loginUser.avatar" class="user-avatar" />
                <CaretDownOutlined class="caret-down-outlined" />
              </div>
              <template #overlay>
                <a-menu>
                  <a-menu-item key="profile">个人中心</a-menu-item>
                  <a-menu-divider />
                  <a-menu-item key="logout" @click="logout">退出登录</a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>

<script setup>
import { h, ref } from 'vue';
import { useRouter } from 'vue-router';
import { CrownOutlined, HomeOutlined, CaretDownOutlined } from '@ant-design/icons-vue';
import { useLoginUserStore } from '@/store/loginUserStore';
import { userLogout } from '@/network/user'
import { message } from 'ant-design-vue'

const router = useRouter();
const loginUserStore = useLoginUserStore();

const menuClick = ({ key }) => {
  console.log("key: ", key);
  router.push(key);
};

const current = ref(['']);

router.afterEach((to, from, failure) => {
  current.value = [to.path];
});

const items = ref([
  {
    key: "/",
    icon: () => h(HomeOutlined),
    label: "主页",
    title: "主页",
  },
  {
    key: "/user/login",
    label: "用户登录",
    title: "用户登录",
  },
  {
    key: "/user/register",
    label: "用户注册",
    title: "用户注册",
  },
  {
    key: "/admin/manage",
    icon: () => h(CrownOutlined),
    label: "用户管理",
    title: "用户管理",
  },
  {
    key: "others",
    label: h(
      "a",
      { href: "https://www.codefather.cn", target: "_blank" },
      "编程导航"
    ),
    title: "编程导航",
  },
]);

const logout = async () => {
  const res = await userLogout();
  if(res.code === 200) {
    const newLoginUser = {
      userName: ""
    };
    loginUserStore.setLoginUser(newLoginUser);
    router.push("/user/login");
  } else {
    message.error(res.message);
  }
}
</script>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title-bar .logo {
  height: 48px;
}

.title-bar .title {
  margin-left: 16px;
  color: black;
  font-size: 18px;
}

.avatar-wrapper {
  position: relative;
  text-align: center;
}

.avatar-wrapper .user-avatar {
  cursor: pointer;
  width: 40px;
  height: 40px;
  border-radius: 10px;
}

.avatar-wrapper .caret-down-outlined {
  cursor: pointer;
  position: absolute;
  right: 0px;
  bottom: 10px;
  font-size: 12px;
}
</style>